package com.xf.system.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xf.system.common.mapper.UserRoleMapper;
import com.xf.system.common.model.UserRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xiefengf
 */
@Slf4j
@Service
public class UserRoleService extends ServiceImpl<UserRoleMapper, UserRole> {

    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 删除用户与角色关联
     * @param userId 用户id
     * @return 执行结果
     */
    public Boolean deleteUserRoleByUid(Long userId) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId,userId);
        return remove(wrapper);
    }

    /**
     * 删除用户与角色关联
     * @param userIdList 用户id集合
     * @return 执行结果
     */
    public Boolean deleteUserRoleByUidList(List<Long> userIdList) {
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserRole::getUserId,userIdList);
        return remove(wrapper);
    }

    /**
     * 通过角色id查询已分配的用户数量
     * @param roleIdList 角色id集合
     * @return 执行结果
     */
    public Integer countUserRoleByRid(List<Long> roleIdList) {
        return userRoleMapper.countUserRoleByRid(roleIdList);
    }

}
